package com.retail.lottery.colalottery.vo;

import com.retail.lottery.colalottery.enums.CommonEnum;
import lombok.*;
import lombok.extern.slf4j.Slf4j;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode
@Slf4j
public class ResultVO<T> {

    private static final Integer FAIL_CODE = 1;
    public static final Integer SUCCESS_CODE = 0;
    public static final Boolean CRYPT = false;
    private static final String SUCCESS_MESSAGE = "success";
    private static final long serialVersionUID = -3382133508072470690L;


    private boolean success;
    private Integer code;
    private String msg;
    private Boolean crypt;
    private T data;


    // 正常的
    private ResultVO(T result) {
        this.success = true;
        this.data = result;
        this.msg = SUCCESS_MESSAGE;
        this.code = SUCCESS_CODE;
        this.crypt = CRYPT;
    }

    // error
    private ResultVO(Integer code, String message, Boolean crypt) {
        this.code = code;
        this.crypt = crypt;
        this.msg = message;
    }

    private ResultVO(Integer code, String message, Boolean crypt, T result) {
        this.code = code;
        this.msg = message;
        this.crypt = crypt;
        this.data = result;
    }

    public static ResultVO<?> succ() {
        return new ResultVO<>(true, SUCCESS_CODE, SUCCESS_MESSAGE, CRYPT, null);
    }


    public static <T> ResultVO<T> succ(T model) {
        return new ResultVO<T>(model);
    }

    public static ResultVO<?> result(boolean res) {
        if (res) {
            return succ();
        }
        return error("ERROR");
    }

    public static ResultVO<?> error(Integer code, String msg) {
        return new ResultVO<>(code, msg, CRYPT);
    }


    public static ResultVO<?> error(CommonEnum error) {
        if (error == null) {
            throw new NullPointerException("CommonEnum is null");
        }
        return new ResultVO<>(error.getResultCode(), error.getResultMsg(), CRYPT);
    }

    public static <T> ResultVO<T> error(CommonEnum error, T data) {
        if (error == null) {
            throw new NullPointerException("CommonEnum is null");
        }
        return new ResultVO<>(error.getResultCode(), error.getResultMsg(), CRYPT, data);
    }

    public static ResultVO<?> error(String msg) {
        return new ResultVO<>(FAIL_CODE, msg, CRYPT);
    }

}
